import os
from flask import jsonify, Blueprint, redirect,request,flash,url_for
from werkzeug.utils import secure_filename


api_upload = Blueprint('api_upload', __name__)
api_upload.url_prefix = "/api/upload"


UPLOAD_FOLDER = '/static/upload/user/aaa'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}


@api_upload.route('/json')
def index():
    json='{"key":"value"}'
    return json
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@api_upload.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            json='{"msg":"no file"}'
            return json
        file = request.files['file']
        # If the user does not select a file, the browser submits an
        # empty file without a filename.
        if file.filename == '':
            json = '{"msg":"No selected file"}'
            return json
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(UPLOAD_FOLDER, filename))
            return '{"msg":"success"}'
    return '{"msg":"fail"}'